package com.first.computerstore.service;

import com.first.computerstore.dao.FavoritesMapper;
import com.first.computerstore.dao.ProductMapper;
import com.first.computerstore.dao.UserMapper;
import com.first.computerstore.entity.Favorites;
import com.first.computerstore.entity.Product;
import com.first.computerstore.query.PageInfoQuery;
import com.first.computerstore.utils.AssertUtil;
import com.first.computerstore.utils.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.first.computerstore.controller.BaseController.OK;

@Service
public class FavoritesService {
    @Resource
    private FavoritesMapper favoritesMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ProductMapper productMapper;

    /*查询当前用户收藏的收藏列表*/
    public PageInfo<Favorites> queryFavorites(Integer status,Integer uid, PageInfoQuery pageInfoQuery) {
        // 开启分页功能
        PageHelper.startPage(pageInfoQuery.getPageNum(),pageInfoQuery.getPageSize());
        // 调用持久层方法进行查询
        List<Favorites> favorites = favoritesMapper.queryFavorites(status,uid);
        // 返回分页数据
        PageInfo<Favorites> pageInfo = new PageInfo<>(favorites);
        return pageInfo;

    }

    /*添加收藏*/
    @Transactional(propagation = Propagation.REQUIRED)
    public int addFavorites(Integer pid, Integer uid) {
        //数据校验
        Product product = productMapper.queryProductByPid(pid);
        AssertUtil.isTrue(null == product,"商品数据不存在，请重试。。。。");
        //新建收藏类，并赋值
        Favorites favorite=new Favorites();
        favorite.setUid(uid);
        favorite.setPid(pid);
        favorite.setImage(product.getImage());
        favorite.setPrice(product.getPrice());
        favorite.setSellPoint(product.getSellPoint());
        favorite.setTitle(product.getTitle());
        favorite.setStatus(1);
        /*FavoritesQuery favoritesQuery = new FavoritesQuery();
        favoritesQuery.setUid(uid);
        favoritesQuery.setImage(product.getImage());
        favoritesQuery.setPid(pid);
        favoritesQuery.setPrice(product.getPrice());
        favoritesQuery.setStatus(1);
        favoritesQuery.setSellPoint(product.getSellPoint());
        favoritesQuery.setTitle(product.getTitle());*/
        //调用dao层方法，执行添加操作，并判断受影响的行数
        int count = favoritesMapper.addFavorites(favorite);
        AssertUtil.isTrue(count==0,"异常，收藏失败");
        return favorite.getFid();
    }
    //取消收藏
    public void cancelFavorites(Integer uid, Integer fid) {


        Integer count = favoritesMapper.cancelFavorites(fid, uid, 0);
        AssertUtil.isTrue(count==0,"服务器异常，取消收藏失败");
    }

    /**
     * 商品详情页 通过商品id查询是否已经加入收藏表中
     * @param pid
     * @return
     */
    public JsonResult<Integer> queryFavoritesByPid(Integer uid,Integer pid) {
        Integer fid = favoritesMapper.queryFavoritesByPid(uid,pid);
        if (fid == null) {
            return new JsonResult<>(400,fid);
        }
        return new JsonResult<>(OK,fid);
    }
}
